/*
 * @Author: haungyan
 * @Date: 2020-11-22 19:46:01
 * @LastEditors: haungyan
 * @LastEditTime: 2020-12-29 10:26:21
 * @Description: file content
 * @FilePath: \fed-e-task-03-01\code\mini-vue\js\vue.js
 */
class Vue {
    constructor(options = {}) {
        this.$options = options
        this.$data = options.data || {}
        this.$methods = options.methods || {}
        this.$el = typeof options.el === 'string' ? document.querySelector(options.el) : options.el

        this._proxyData(this.$data)
        this._proxyMethods(this.$methods)
        new Observer(this.$data)
        new Compiler(this)
    }
    _proxyData(data) {
        Object.keys(data).forEach(key => {
            Object.defineProperty(this, key, {
                configurable: true,
                enumerable: true,
                get() {
                    return data[key]
                },
                set(newValue) {
                    if (newValue === data[key]) {
                        return
                    }
                    data[key] = newValue
                }
            })
        })
    }
    _proxyMethods(methods) {
        Object.keys(methods).forEach(key => {
            this[key] = methods[key]
        })
    }
}